home *** CD-ROM | disk | FTP | other *** search
/ Mac Magazin/MacEasy 34 / Mac Magazin and MacEasy Magazine CD - Issue 34.iso / Grafik & Text / Alpha ƒ / Tcl / Modes / latexSmart.tcl < prev    next >
Text File  |  1996-08-15  |  6KB  |  211 lines

  1. #############################################################################
  2. #############################################################################
  3. #
  4. # latexSmart.tcl (called from latex.tcl)
  5. #
  6. # Smart quotes, dots, subscripts, and superscripts
  7. #
  8. #############################################################################
  9. #
  10. # Original author unknown
  11. #
  12. # Maintainer:  Tom Scavo <trscavo@syr.edu>
  13. #
  14. #############################################################################
  15. #############################################################################
  16.  
  17. #--------------------------------------------------------------------------
  18. # Smart quotes:
  19. #--------------------------------------------------------------------------
  20.  
  21. proc smartDQuote {} {
  22.     global TeXmodeVars
  23.     if {[isSelection]} { deleteSelection }
  24.     if { !$TeXmodeVars(smartQuotes) || [literalChar] } { insertText {"}; return }
  25.     if {[leftQ]} {
  26.         insertText {``}
  27.     } else {
  28.         insertText {''}
  29.     }
  30. }
  31.  
  32. # bind double quote:
  33. ascii 0x22 <s> smartDQuote TeX
  34.  
  35. proc smartQuote {} {
  36.     global TeXmodeVars
  37.     if {[isSelection]} { deleteSelection }
  38.     if { !$TeXmodeVars(smartQuotes) || [literalChar]  } { insertText {'}; return }
  39.     if {[leftQ]} {
  40.         insertText {`}
  41.     } else {
  42.         insertText {'}
  43.     }
  44. }
  45.  
  46. # bind single quote:
  47. ascii 0x27 smartQuote TeX
  48.  
  49. proc leftQ {} {
  50.     if { [getPos] == 0 } { return 1 };
  51.     set q [lookAt [expr [getPos]-1]]
  52.     case $q in {
  53.         {\t}  {return 1}
  54.         {(}  {return 1}
  55.         {\{}  {return 1}
  56.         {[}  {return 1}
  57.         {<}  {return 1}
  58.         {\ } {return 1}
  59.         {\r} {return 1}
  60.     }
  61.     return 0
  62. }
  63.  
  64. #--------------------------------------------------------------------------
  65. # Smart dots:
  66. #--------------------------------------------------------------------------
  67.  
  68. # proc smartDots {} {
  69. #     global TeXmodeVars
  70. #     if {[isSelection]} { deleteSelection }
  71. #     if { !$TeXmodeVars(smartDots) || [literalChar] } { insertText {.}; return }
  72. #     # Labels contain literal dots:
  73. #     set pat {\\(label|(page|eq)?ref|bibitem|(no)?cite)(\[[^][]*\])?\{}
  74. #     if { [findPatJustBefore "$pat" "${pat}\[\]\[()`'.,:;?!a-zA-Z0-9/@*-\]*\$"] != "" } {
  75. #         insertText "."
  76. #         return
  77. #     }
  78. #     # Filenames contain literal dots:
  79. #     set pat {\\(usepackage|input|include(only)?|documentclass|bibliography(style)?|LoadClass|RequirePackage|begin\{filecontents\})(\[[^][]*\])?\{}
  80. #     if { [findPatJustBefore "$pat" "${pat}\[.:a-zA-Z_0-9/-\]*\$"] != "" } {
  81. #         insertText "."
  82. #         return
  83. #     }
  84. #     if {[lookAt [expr [set endPos [getPos]]-1]] == "."} then {
  85. #         if {[lookAt [set begPos [expr $endPos-2]]] == "."} then {
  86. #             replaceText $begPos $endPos "\\ldots"
  87. #         } else {
  88. #             insertText "."
  89. #         }
  90. #     } else {
  91. #         insertText "."
  92. #     }
  93. # }
  94. proc smartDots {} {
  95.     global TeXmodeVars
  96.     if {[isSelection]} { deleteSelection }
  97.     if { !$TeXmodeVars(smartDots) || [literalChar] } { insertText {.}; return }
  98.     if {[lookAt [expr [set endPos [getPos]]-1]] == "."} then {
  99.         if {[lookAt [set begPos [expr $endPos-2]]] == "."} then {
  100.             replaceText $begPos $endPos "\\ldots"
  101.         } else {
  102.             insertText "."
  103.         }
  104.     } else {
  105.         insertText "."
  106.     }
  107. }
  108.  
  109. # bind period:
  110. ascii 0x2e smartDots "TeX"
  111.  
  112. #--------------------------------------------------------------------------
  113. # Smart subscripts and superscripts:
  114. #--------------------------------------------------------------------------
  115.  
  116. proc smartSubscripts {} {
  117.     smartScripts {_}
  118. }
  119.  
  120. proc smartSuperscripts {} {
  121.     smartScripts {^}
  122. }
  123.  
  124. proc smartScripts {char} {
  125.     global TeXmodeVars
  126.     if {[isSelection]} { deleteSelection }
  127.     if { !$TeXmodeVars(smartScripts) || [literalChar] } then {
  128.         insertText $char
  129.         return
  130.     }
  131.     # Filenames contain literal underscores:
  132.     set pat {\\(usepackage|input|include(only)?|documentclass|bibliography(style)?|LoadClass|RequirePackage|begin\{filecontents\})(\[[^][]*\])?\{}
  133.     if { [findPatJustBefore "$pat" "${pat}\[.:a-zA-Z0-9/^_-\]*\$"] != "" } {
  134.         insertText $char
  135.         return
  136.     }
  137.     if { $TeXmodeVars(smartScripts) } then {
  138.         if { $char == {_} } { subscript } { superscript }
  139.     }
  140. }
  141.  
  142. # bind underscore and caret:
  143. ascii 0x5f <s> smartSubscripts "TeX"
  144. ascii 0x5e <s> smartSuperscripts "TeX"
  145.  
  146. #--------------------------------------------------------------------------
  147. # Escapes and exceptions:
  148. #--------------------------------------------------------------------------
  149.  
  150. # This proc has been moved to procs.tcl:
  151. # proc literalChar {} {
  152. #     return [expr {[lookAt [expr [getPos] - 1]] == "\\"}]
  153. # }
  154. proc escapeSmartStuff {} {
  155.     if {![isSelection]} then {
  156.         set pos [getPos]
  157.         # Escape double quotes:
  158.         if { $pos > 1 } then {
  159.             set pos2 [expr $pos - 2]
  160.             if { [getText $pos2 $pos] == "''" } then {
  161.                 replaceText $pos2 $pos {"}
  162.                 return
  163.             } elseif { [getText $pos2 $pos] == "``" } then {
  164.                 replaceText $pos2 $pos {"}
  165.                 return
  166.             }
  167.         }
  168.         # Escape single quote:
  169.         if { $pos > 0 } then {
  170.             set pos1 [expr $pos - 1]
  171.             if { [lookAt $pos1] == "`" } then {
  172.                 backSpace
  173.                 insertText {'}
  174.                 return
  175.             }
  176.         }
  177.         # Escape dots:
  178.         if { $pos > 5 } then {
  179.             set pos6 [expr $pos - 6]
  180.             if { [getText $pos6 $pos] == "\\ldots" } then {
  181.                 replaceText $pos6 $pos {...}
  182.                 return
  183.             }
  184.         }
  185.         # Escape underscore:
  186.         if { $pos > 1 && [maxPos] > [expr $pos + 1] } then {
  187.             set begPos [expr $pos - 2]
  188.             set endPos [expr $pos + 2]
  189.             if { [getText $begPos $endPos] == "_\{\}•" } then {
  190.                 replaceText $begPos $endPos {_}
  191.                 return
  192.             }
  193.         }
  194.         # Escape caret:
  195.         if { $pos > 1 && [maxPos] > [expr $pos + 1] } then {
  196.             set begPos [expr $pos - 2]
  197.             set endPos [expr $pos + 2]
  198.             if { [getText $begPos $endPos] == "^\{\}•" } then {
  199.                 replaceText $begPos $endPos {^}
  200.                 return
  201.             }
  202.         }
  203.     }
  204.     backSpace
  205. }
  206.  
  207. # bind delete key:
  208. ascii 0x08 escapeSmartStuff "TeX"
  209.  
  210.